#include "Common.fx"

//--------------------------------------
struct a2v {
	a2vApplyBasic basic;
};

struct v2f {
	v2fApplyBasic basic;
};

//--------------------------------------
sampler ProjLightMapSamp : register(s2);
sampler ShadowMapSamp : register(s3);

const float g_VsmEpsilon = 0.00005f;

//--------------------------------------
v2f vsMain(a2v input) {
	
	v2f output;
	
	ApplyBasicVertexProcess(output.basic, input.basic);
	
	return output;
}

//--------------------------------------
f2fb psMain(v2f input) {

	f2fb output;
	
	input.basic.lsPos.xy /= input.basic.lsPos.w;
	input.basic.lsPos.xy = saturate(input.basic.lsPos.xy);
	
	float4 lightMap = tex2D(ProjLightMapSamp, input.basic.lsPos.xy);
	
	if(input.basic.lsPos.z < 0.01)
		lightMap = 0;
	
	float2 smTexcoord;
	ShadowMapBaseTexcoord(smTexcoord, input.basic.lsPos.xy);
	float4 moments = tex2D(ShadowMapSamp, smTexcoord);
	
	float farestDepth = 1.0;
	
	for(int i=0; i<4; ++i) {
	
		if(moments[i] < input.basic.lsPos.z)
			farestDepth = moments[i];
	
	}
	
	//ApplyBasicFragmentProcess(output.c0, input.basic, lightMap);
	
	output.c0 = float4(farestDepth > input.basic.lsPos.z, input.basic.lsPos.z, 0, 0);
	
	return output;
}

//--------------------------------------
technique Apply {

	pass {
	
		AlphaBlendEnable	= false;
		AlphaTestEnable		= false;
		
		MagFilter[3]		= Linear;
		MinFilter[3]		= Linear;
		
		VertexShader		= compile vs_3_0 vsMain();
		PixelShader			= compile ps_3_0 psMain();
	}
}

//--------------------------------------